package com.scorpio.callback.api;

import com.scorpio.pay.domain.PayCallbackEntity;
import com.scorpio.pay.enums.PayTypeEnum;
import com.scorpio.pay.service.PayCallbackService;
import com.scorpio.sdk.constant.WeiXinConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay_callback")
public class PayCallbackController {

    @Autowired
    private PayCallbackService payCallbackService;

    @RequestMapping(value = "/wxpay", method = {RequestMethod.POST})
    public String wxpay(@RequestBody String notifyData) {
        PayCallbackEntity entity = new PayCallbackEntity();
        entity.setBody(notifyData);
        entity.setType(PayTypeEnum.WeChatPay.getValue());
        if (payCallbackService.save(entity) > 0) {
            if (payCallbackService.handlePay(entity)) {
                return WeiXinConstant.getPaySuccessResult();
            }
        }
        return WeiXinConstant.getPayFAILResult();
    }

}
